package com.vma.inter.controller.lottery;

import com.jfinal.core.Controller;
import com.vma.advertisement.model.Advertisement;
import com.vma.advertisement.model.Message;
import com.vma.inter.util.ReturnsUtil;
import com.vma.lottery.model.LotteryRecord;
import com.vma.lottery.model.Predict;
import com.vma.lottery.vo.LotteryRecordVo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenzui on 2017/4/4.
 */
public class IndexController extends Controller {
    public void init(){
        try {
            SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");

            Predict predict = Predict.dao.findFirst("select * from predict order by id desc");

            LotteryRecordVo lotteryRecordVo = LotteryRecord.dao.gryLatestRecord();

            LotteryRecord lotteryRecord = LotteryRecord.dao.findFirst("select * from lottery_record order by opentime desc");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(lotteryRecord.getOpentime());
            int day = calendar.get(Calendar.DAY_OF_WEEK);
            switch (day){
                case 1:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+2); break;
                case 2:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+1); break;
                case 3:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+2); break;
                case 4:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+1); break;
                case 5:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+2); break;
                case 6:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+1); break;
                case 7:calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+3); break;

            }
            String str = "第"+(lotteryRecord.getExpect()+1)+"期 "+sdFormatter.format(calendar.getTime())+
                    " 21点30分 星期"+getDay(calendar.get(Calendar.DAY_OF_WEEK));

            List<Advertisement> advertisementList = Advertisement.dao.find("select id,imgUrl,if_link,title,type,url from advertisement where if_delete = 0" +
                    " and a_status = 1 and type = 1 order by create_time desc");

            List<Message> messageList = Message.dao.find("select id,content,url from message WHERE if_delete=0 AND a_status =1" +
                    " order by create_time desc");

            Map<String,Object> result = new HashMap<String, Object>();
            result.put("advertisementList",advertisementList);
            result.put("messageList",messageList);
            result.put("lotteryRecord",lotteryRecordVo);
            result.put("nextTime",str);
            result.put("predictTitle",sdFormatter.format(calendar.getTime()) +" 第"+(lotteryRecord.getExpect()+1)+"期 预测");
            result.put("predict",predict);
            renderJson(ReturnsUtil.returnSuccess(result));


        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    private String getDay(int day){
        String reStr="";
        switch (day){
            case 1:reStr="日";break;
            case 2:reStr="一";break;
            case 3:reStr="二";break;
            case 4:reStr="三";break;
            case 5:reStr="四";break;
            case 6:reStr="五";break;
            case 7:reStr="六";break;
        }
        return reStr;
    }
}
